package com.taotao.controller;

import com.google.common.util.concurrent.RateLimiter;
import com.taotao.service.SWRateLimiter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wangjin
 * @title: ServiceCurrentLimitController
 * @projectName sw-gateway
 * @description: guaua 平滑限流
 * @date 2021/12/9 15:12
 */
@RestController
@Slf4j
public class ServiceCurrentLimitController {

    /**
     * 限流 qps 1 每秒只会向令牌桶中投射一个令牌
     */
    private RateLimiter rateLimiter = RateLimiter.create(1);


    /**
     * 根据Ratelimiter限流
     */
    @RequestMapping("/limit")
    public String limit() {
        if (!rateLimiter.tryAcquire()) {
            log.info("接口限流了");
            return "您访问次数过多，请稍后重试!";
        }
        return "success";
    }

    @RequestMapping("/getMeite")
    @SWRateLimiter(aqs = 2, msg = "当前接口已经被限流")
    public String getMeite() {
        return "getMayikt";
    }

    @RequestMapping("/gMeite")

    public String gMeite() {
        return "getMayikt";
    }
}
